<?php

namespace App\Orchid\Screens\Setting;

use App\Models\Setting;
use Orchid\Screen\Screen;
use Orchid\Screen\Actions\Link;
use Orchid\Support\Facades\Toast;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\TD;

class SettingListScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        return [
            'settings' => Setting::orderBy('key')
                ->paginate(),
        ];
    }

    /**
     * The name of the screen displayed in the header.
     */
    public function name(): ?string
    {
        return '系统设置';
    }

    /**
     * Display header description.
     */
    public function description(): ?string
    {
        return '管理系统配置参数';
    }

    /**
     * The screen's action buttons.
     *
     * @return \Orchid\Screen\Action[]
     */
    public function commandBar(): iterable
    {
        return [
            Link::make('新建设置')
                ->icon('plus')
                ->route('platform.settings.create'),
        ];
    }

    /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]
     */
    public function layout(): iterable
    {
        return [
            Layout::table('settings', [
                TD::make('key', '设置键')
                    ->sort()
                    ->filter(TD::FILTER_TEXT)
                    ->render(function (Setting $setting) {
                        return Link::make($setting->key)
                            ->route('platform.settings.edit', $setting);
                    }),
                    
                TD::make('name', '设置名称')
                    ->sort()
                    ->filter(TD::FILTER_TEXT),
                    
                TD::make('value', '设置值')
                    ->render(function (Setting $setting) {
                        $value = $setting->value;
                        if (strlen($value) > 50) {
                            return substr($value, 0, 50) . '...';
                        }
                        return $value;
                    }),
                    
                TD::make('type', '类型')
                    ->render(function (Setting $setting) {
                        return match($setting->type ?? 'text') {
                            'text' => '文本',
                            'textarea' => '多行文本',
                            'number' => '数字',
                            'boolean' => '布尔值',
                            'select' => '选择',
                            'json' => 'JSON',
                            default => $setting->type,
                        };
                    }),
                    
                TD::make('group', '分组')
                    ->render(function (Setting $setting) {
                        return $setting->group ?: '默认';
                    }),
                    
                TD::make('created_at', '创建时间')
                    ->sort()
                    ->render(function (Setting $setting) {
                        return $setting->created_at->format('Y-m-d H:i');
                    }),
                    
                TD::make('操作')
                    ->align(TD::ALIGN_CENTER)
                    ->width('150px')
                    ->render(function (Setting $setting) {
                        return Link::make('编辑')
                            ->icon('pencil')
                            ->route('platform.settings.edit', $setting);
                    }),
            ]),
        ];
    }
    
    /**
     * Remove setting
     *
     * @param Setting $setting
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function remove(Setting $setting)
    {
        $setting->delete();
        
        Toast::info('设置已删除');
    }
}